﻿using Sys.Common;
using Sys.Core.Sys;
using Sys.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace Sys.Api.Sys
{
    public class SettingApi : BaseApi
    {
        public string Save()
        {
            JsonMsg result = new JsonMsg();

            int record = 0; HttpContext context = HttpContext.Current;

            string[] settings = { "SysName", "SiteKey", "Copyright" };

            for (int i = 0; i < settings.Length; i++)
            {
                record += SettingBll.I.SetSetting("Default", settings[i], context.Request.Form[settings[i]]);
            }

            if (record == settings.Length) { result.Success = true; result.Message = "设置成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string Website()
        {
            JsonMsg result = new JsonMsg();

            int record = 0; HttpContext context = HttpContext.Current;

            string[] settings = { "SiteName", "Keywords", "Description", "CustomerService", "VideoFront", "VideoFlv", "Copyright" };

            for (int i = 0; i < settings.Length; i++)
            {
                record += SettingBll.I.SetSetting("Website", settings[i], context.Request.Form[settings[i]]);
            }

            if (record == settings.Length) { result.Success = true; result.Message = "设置成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string ImgVideo()
        {
            Hashtable hash = KindHelper.ImgUpload();

            if (Convert.ToInt32(hash["error"]) == 0)
            {
                HttpContext context = HttpContext.Current;

                string filename = hash["url"].ToString();

                string temppath = context.Server.MapPath("~/userfiles/temp/" + filename);
                string filepath = context.Server.MapPath("~/userfiles/website/");

                if (!Directory.Exists(filepath)) { Directory.CreateDirectory(filepath); }

                ThumNail.MakeThumNail(temppath, filepath + filename, 218, 140, TnType.Cut);

                System.IO.File.Delete(temppath);

                hash["url"] = filename;
            }

            return JsonHelper.ToJson(hash);
        }

        public string FlvVideo()
        {
            Hashtable hash = KindHelper.FileUpload("website");

            return JsonHelper.ToJson(hash);
        }
    }
}
